package com.situ.crm2026.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.crm2026.model.Member;
import com.situ.crm2026.model.MemberCourseReservation;
import com.situ.crm2026.model.search.MemberSearchBean;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MemberMapper extends BaseMapper<Member> {
    //查询全部
    default Page<Member> findAll(Page<Member> page, MemberSearchBean msb) {
        //定义查询器
        LambdaQueryWrapper<Member> qw= Wrappers.lambdaQuery(msb);
        if(msb.getBirthdayFrom()!=null){
            qw.ge(Member::getBirthday,msb.getBirthdayFrom());
        }
        if(msb.getBirthdayTo()!=null){
            qw.lt(Member::getBirthday,msb.getBirthdayTo());
        }
        return selectPage(page,qw);
    }
    //批量删除课程

    // 查询会员的预约信息及课程详情
    @Select("SELECT mcr.*, c.id as course_id, c.name as course_name, c.coach, c.room " +
            "FROM member_course_reservation mcr " +
            "LEFT JOIN courses c ON mcr.course_id = c.id " +
            "WHERE mcr.member_id = #{memberId} and mcr.status!=2 and mcr.course_type=0")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "memberId", column = "member_id"),
            @Result(property = "courseId", column = "course_id"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "status", column = "status"),
            @Result(property = "course.id", column = "course_id"),
            @Result(property = "course.name", column = "course_name"),
            @Result(property = "course.coach", column = "coach"),
            @Result(property = "course.room", column = "room")
    })
    List<MemberCourseReservation> findReservationsByMemberIdWithCourse(@Param("memberId") Integer memberId);
}
